/*
 * @LastEditTime: 2023-02-28 14:36:34
 */
import { router } from "../index";
import { Singer } from "../../Models/Singer.schema";
import { SingerController } from "../../Controller/Singer.controller";
import { createSingerDTO, updateSingerDTO } from "../../DTO/Singer.dto";

const SingerModel = new SingerController(Singer);

//获取歌手
router.get("/singer", async (ctx, next) => {
  if (ctx.request.query.title) {
    const res = (await SingerModel.findByTitle(
      ctx.request.query.title.toString()
    )) as { code: number; message: string };
    res.code === 400 ? (ctx.status = 400) : null;
    ctx.body = res;
  } else if (ctx.request.query.id) {
    ctx.body = await SingerModel.findById(ctx.request.query.id.toString());
  }
});
//获取全部歌手
router.get("/singer/find", async (ctx, next) => {
  if (ctx.request.query.type && ctx.request.query.area && ctx.request.query.name)
    // console.log(ctx.request.query.type, ctx.request.query.area, ctx.request.query.name)
    ctx.body = await SingerModel.findAll(Number(ctx.request.query.type), Number(ctx.request.query.area), ctx.request.query.name?.toString());
});
//获取歌手热门歌曲
router.get("/singer/hot", async (ctx, next) => {
  if (ctx.request.query.id) {
    ctx.body = await SingerModel.getHot(ctx.request.query.id.toString())
  }
});
//获取全部歌手
router.get("/singer/introdeuce", async (ctx, next) => {
  if (ctx.request.query.id) {
    ctx.body = await SingerModel.getIntroduce(ctx.request.query.id.toString())
  }
});

// 创建歌手
router.post("/singer", async (ctx, next) => {
  console.log(ctx.request.body);
  ctx.body = await SingerModel.createItem(ctx.request.body as createSingerDTO);
});

//删除歌手
router.delete("/singer/:id", async (ctx, next) => {
  ctx.body = await SingerModel.removeById(ctx.params.id);
});

// 更新歌手
router.patch("/singer/:id", async (ctx, next) => {
  if (ctx.params.id)
    ctx.body = await SingerModel.updateById(
      ctx.params.id,
      ctx.request.body as updateSingerDTO
    );
});
